package com.graducationdesign.springboot.task;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.graducationdesign.springboot.entities.crawling.RecruitmentInformation;
import com.graducationdesign.springboot.service.RecruitmentInformationService;
import com.graducationdesign.springboot.util.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Mr.zhneg
 * @create 2020-05-21-23:59
 */
@Component
public class RecruitmentInformationTask{
    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());

    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private RecruitmentInformationService itemService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    //间隔100s执行一次任务
   @Scheduled(fixedDelay = 100*1000)
    public void  itemTask() throws Exception{
        //1.声明地址
//        String url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&suggest=1.def.0.V18--12s0%2C20s0%2C38s0%2C97s0&wq=shouji&s=122&click=0&page=1";
        String url = "https://xy.51job.com/default-xs.php";

        //2.解析页面

            String html = httpUtils.doGetHtml(url );
            //解析页面，获取商品数据并存储
            parse(html);


        System.out.println("数据抓取完成！");

    }

    /**
     * //解析页面，获取商品数据并存储
     * @param html
     */
    private void parse(String html) throws Exception{
        //1.解析html
//        Jsoup可以解析HTML得到Document文档对象，
// 通常我们需要爬取的内容是文档中的某些东西而不是整个HTML内容，
// 这时就需要使用select选择器来提取我们需要的内容，
// select选择器的使用类似于jquery的使用方式。
// 注意：下面爬取的是a标签的内容，这个根据爬取的网站的升级变更而不准确。

        Document doc = Jsoup.parse(html);

        logger.info(String.valueOf(doc));
        //2.获取招聘信息
        Elements spuEles = doc.select("div.ctxt > div.cell > div.e");
        logger.info(String.valueOf(spuEles));

        for (Element spuEle : spuEles){
            //获取招聘标题
            String topic = spuEle.select("[target]").attr("title");
            //获取招聘公司url
            String url = spuEle.select("[target]").attr("href");

            //获取招聘公司名字
            Elements names = spuEle.select("span");
            String name=names.get(0).text();


            //获取招聘公司所在的位置
            String location = spuEle.select("[class]").attr("title");
           logger.info(topic,name,url,location);

            RecruitmentInformation recruitmentInformation=new RecruitmentInformation();

            recruitmentInformation.setCreated(new Date());
            recruitmentInformation.setCompanylocation(location);
            recruitmentInformation.setUrl(url);
            recruitmentInformation.setRecruitmenttopic(topic);
            recruitmentInformation.setRecruitmentcompany(name);

            //8.保存数据库
            itemService.save(recruitmentInformation);
            logger.info("成功保存");

        }
    }


}
